package main

import (
	"fmt"
	"net"
)

const NetProtocol string = "tcp"

type Server struct {
	Ip   string
	Port int
}

func NewServer(ip string, port int) *Server {
	server := &Server{Ip: ip, Port: port}
	return server
}

func (server *Server) Handler(conn net.Conn) {
	fmt.Println("建立连接...")
}

func (server *Server) Start() {
	// 1. 开启socket的监听
	listener, err := net.Listen(NetProtocol, fmt.Sprintf("%s:%d", server.Ip, server.Port))
	if err != nil {
		fmt.Println("err =", err)
		return
	}

	// 程序最后关闭socket连接
	defer listener.Close()

	for {
		conn, err2 := listener.Accept()
		if err2 != nil {
			fmt.Println("err =", err)
			continue
		}

		go server.Handler(conn)
	}

}
